package com.gsafety.wellsfort.service.knowledge;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gsafety.wellsfort.common.context.LoginContextUtil;
import com.gsafety.wellsfort.common.util.ApiResponse;
import com.gsafety.wellsfort.dao.knowledge.KnowledgePointDAO;
import com.gsafety.wellsfort.dao.security.AdminDAO;
import com.gsafety.wellsfort.domain.dto.knowledgepoint.KnowledgePointDTO;
import com.gsafety.wellsfort.domain.dto.knowledgepoint.KnowledgePointQueryDTO;
import com.gsafety.wellsfort.domain.dto.question.QuestionQueryDTO;
import com.gsafety.wellsfort.domain.model.knowledge.AnswerOption;
import com.gsafety.wellsfort.domain.model.knowledge.KnowledgePoint;
import com.gsafety.wellsfort.domain.model.knowledge.Question;
import com.gsafety.wellsfort.domain.model.security.Admin;
import com.gsafety.wellsfort.domain.vo.KnowledgePointVO;
import com.gsafety.wellsfort.domain.vo.QuestionVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class KnowledgePointService {
    @Autowired(required = false)
    KnowledgePointDAO knowledgeDAO;
    @Autowired(required = false)
    AdminDAO adminDAO;
    public ApiResponse<KnowledgePointDTO> saveKnowledgePoint(KnowledgePointDTO knowledgePointDto){
        ApiResponse apiResponse=null;
        KnowledgePoint knowledgePoint=new KnowledgePoint();
        BeanUtils.copyProperties(knowledgePointDto,knowledgePoint);
        Admin admin=LoginContextUtil.getAdmin();
        knowledgePoint.setUpdateTime(new Date());
        if(admin!=null)knowledgePoint.setModified(admin.getId());
        if(knowledgePointDto.getId()!=null){
            knowledgeDAO.updateByPrimaryKeySelective(knowledgePoint);
        }else{
            knowledgePoint.setCreateTime(new Date());
            if(admin!=null)knowledgePoint.setCreator(admin.getId());
            knowledgeDAO.insert(knowledgePoint);
            knowledgePointDto.setId(knowledgePoint.getId());
        }
        apiResponse=ApiResponse.success(knowledgePointDto);
        return apiResponse;
    }
    public ApiResponse deleteKnowledgePoint(Integer id){
        ApiResponse apiResponse=null;
        try{
            knowledgeDAO.deleteByPrimaryKey(id);
            apiResponse=ApiResponse.success("知识点删除成功");
        }catch (Exception e){
            apiResponse=ApiResponse.error();
            e.printStackTrace();
        }
        return apiResponse;
    }
    public ApiResponse<KnowledgePointVO> getKnowledgePoint(Integer id){
        ApiResponse<KnowledgePointVO> apiResponse=null;
        try{
            KnowledgePoint knowpoint= knowledgeDAO.selectByPrimaryKey(id);
            KnowledgePointVO qVo =new KnowledgePointVO();
            BeanUtils.copyProperties(knowpoint,qVo);
            if(qVo.getModified()!=null){
                Admin admin=adminDAO.selectByPrimaryKey(qVo.getModified());
                if(admin!=null)qVo.setModifiedName(admin.getName());
            }
            apiResponse=ApiResponse.success(qVo);
        }catch (Exception e){
            apiResponse=ApiResponse.error();
            e.printStackTrace();
        }
        return apiResponse;
    }
    public ApiResponse<List<KnowledgePointVO>> findByPageParams(KnowledgePointQueryDTO dtoParams) {
        ApiResponse<List<KnowledgePointVO>> apiResponse;
        Admin admin=LoginContextUtil.getAdmin();
        if(dtoParams.getResourceScope()!=null&&dtoParams.getResourceScope()!=0){
            dtoParams.setCreator(admin.getId());
        }
        Page page= PageHelper.startPage(dtoParams.getPageNo(), dtoParams.getPageSize());
        List<KnowledgePoint> resultList= knowledgeDAO.selectByParam(dtoParams);
        List<KnowledgePointVO> voList=new ArrayList<>();
        KnowledgePointVO qVo ;

        for(KnowledgePoint q:resultList){
            qVo =new KnowledgePointVO.VOBuilder(q).build();
            if(qVo.getModified()!=null){
                admin=adminDAO.selectByPrimaryKey(qVo.getModified());
                if(admin!=null)qVo.setModifiedName(admin.getName());
            }
            voList.add(qVo);
        }
        PageInfo pageInfo= new PageInfo(page);
        pageInfo.setList(resultList);
        apiResponse=ApiResponse.success(voList,(int)pageInfo.getTotal());
        return apiResponse;
    }
}
